
/* @file			  alarm.c
* @author  		Piyumal Ranawaka st20000427
* @version 		0.2
* @date    		November 15, 2014
* @brief   		To blink led's as an alarm with digital output ports
*/

/* Includes ------------------------------------------------------------------*/
#include "alarm.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
#define digitalPinToPort(P) 	( DIGITAL_PIN_TO_PORT[P] )
#define digitalPinToBitMask(P) 	( DIGITAL_PIN_TO_BIT_MASK[P] )

/* Private variables ---------------------------------------------------------*/
GPIO_TypeDef *DIGITAL_PIN_TO_PORT[4] = {GPIOA,GPIOA,GPIOA,GPIOA};


uint16_t DIGITAL_PIN_TO_BIT_MASK[4] = {GPIO_Pin_1,GPIO_Pin_2, GPIO_Pin_3, GPIO_Pin_4};


/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/



/**
* @brief	Initialization code to configure ports for LED's as digital outputs
* @param	pin no (1/2/3/4)
* @retval	None
*/
void alarm_config(uint8_t pin)
{
	GPIO_InitTypeDef gpio_init_struct;
	GPIO_TypeDef *gpio_port = digitalPinToPort(pin);
	uint16_t gpio_pin = digitalPinToBitMask(pin);
	
	
		gpio_init_struct.GPIO_Mode = GPIO_Mode_OUT;
		gpio_init_struct.GPIO_OType = GPIO_OType_PP;
		gpio_init_struct.GPIO_Speed = GPIO_Speed_Level_3;
		gpio_init_struct.GPIO_PuPd = GPIO_PuPd_NOPULL;
		gpio_init_struct.GPIO_Pin = gpio_pin;
	
	GPIO_Init(gpio_port, &gpio_init_struct);
}

/**
* @brief	Write a HIGH or a LOW value to a digital pin.
* @param	pin		the pin number
* @param	value	HIGH or LOW
* @retval	None
*/
void digitalWrite(uint8_t pin, int value)
{
	GPIO_TypeDef *gpio_port = digitalPinToPort(pin);
	uint16_t gpio_pin = digitalPinToBitMask(pin);
	
	if (value == HIGH) {
		GPIO_WriteBit(gpio_port, gpio_pin, Bit_SET); 
	} else {
		GPIO_WriteBit(gpio_port, gpio_pin, Bit_RESET);
	}
}


/**
* @brief	code to blink leds for 1s on command
* @param	pin no (1/2/3/4)
* @retval	None
*/
void led_blink(uint8_t pin)
{
		digitalWrite(pin, HIGH);
		delay (1000);
		digitalwrite(pin,LOW);
}


